package com.treasure.mall.biz.service.core.banner.impl;

import com.treasure.mall.biz.base.AbstractSearchableCrudService;
import com.treasure.mall.biz.base.SpecificationAndSort;
import com.treasure.mall.biz.dto.BaseSaveDTO;
import com.treasure.mall.biz.dto.BaseSearchDTO;
import com.treasure.mall.biz.dto.common.BannerSaveDTO;
import com.treasure.mall.biz.entity.banner.BannerPO;
import com.treasure.mall.biz.mapstruct.BannerMapStruct;
import com.treasure.mall.biz.repository.banner.BannerRepository;
import com.treasure.mall.biz.service.core.banner.BannerService;
import com.treasure.mall.biz.vo.banner.BannerVO;
import com.treasure.mall.zbq.common.ObjectUtils;
import com.treasure.mall.zbq.common.ienum.banner.BannerType;
import com.treasure.mall.zbq.common.ienum.base.CodeDescEnumHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author guomw
 * @version 1.0
 * @date 2019/10/26 16:25
 */
@Service
public class BannerServiceImpl extends AbstractSearchableCrudService<BannerPO, Long> implements BannerService {

    private BannerRepository bannerRepository;

    @Autowired
    protected BannerServiceImpl(BannerRepository repository) {
        super(repository);
        bannerRepository = repository;
    }

    @Override
    protected SpecificationAndSort<BannerPO> specificationAndSort(BaseSearchDTO searchDTO) {
        Specification<BannerPO> specification = (root, query, cb) -> {
            List<Predicate> predicates = new ArrayList<>();
            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
        };
        Sort sort = new Sort(Sort.Direction.ASC, "sortNum");

        return new SpecificationAndSort<>(specification, sort);
    }

    @Override
    public BannerPO save(BaseSaveDTO saveDTO) {
        BannerSaveDTO bannerSaveDTO = (BannerSaveDTO) saveDTO;
        BannerPO po = BannerMapStruct.INSTANCE.dto2po(bannerSaveDTO);
        po.setSortNum(bannerSaveDTO.getSortNum());
        BannerType bannerType = CodeDescEnumHelper.getEnumTypeByCode(BannerType.class, bannerSaveDTO.getType());
        po.setBannerType(bannerType);
        if (po.getId() == null || po.getId() == 0) {
            po.setCreateTime(LocalDateTime.now());
        }
        return bannerRepository.save(po);
    }

    /**
     * 获取广告列表
     *
     * @return List
     */
    @Override
    public List<BannerVO> getList() {
        List<BannerPO> pos = bannerRepository.findAll(new Sort(Sort.Direction.ASC, "sortNum"));
        if (!ObjectUtils.isEmpty(pos)) {
            return BannerMapStruct.INSTANCE.pos2vos(pos);
        }
        return new ArrayList<>();

    }

    /**
     * 删除广告
     *
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        bannerRepository.deleteById(id);
    }


}
